﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using Microsoft.Win32;

namespace OOLibTestApp
{
   /// <summary>
   /// Interaction logic for MainWindow.xaml
   /// </summary>
   public partial class MainWindow : Window
   {
      public MainWindow()
      {
         Docs = new ObservableCollection<OOLib.OfficeDocument>();

         InitializeComponent();
      }

      public ObservableCollection<OOLib.OfficeDocument> Docs { get; private set; }

      private OOLib.ServiceManager _ServiceManager = null;

      public OOLib.Desktop Desktop { get; private set; }

      private void Window_Loaded(object sender, RoutedEventArgs e)
      {
         Init();
      }

      private void Window_Closed(object sender, EventArgs e)
      {

      }

      private void Init()
      {
         try
         {
            if (_ServiceManager == null)
            {
               Results.AppendText("Init ... ");
               _ServiceManager = new OOLib.ServiceManager();
               Desktop = _ServiceManager.CreateDesktop();
               Results.AppendText("done\n");
            }
         }
         catch (Exception ex)
         {
            Results.AppendText("failed\n" + ex.ToString() + "\n");
         }
      }

      private void NewTextDocButton_Click(object sender, RoutedEventArgs e)
      {
         try
         {
            if (Desktop != null)
            {
               Results.AppendText("New TextDoc ... ");
               var doc = Desktop.CreateTextDocument();
               Docs.Add(doc);
               DocList.SelectedItem = doc;
               Results.AppendText("done\n");
            }
         }
         catch (Exception ex)
         {
            Results.AppendText("failed\n" + ex.ToString() + "\n");
         }
      }

      public OOLib.OfficeDocument SelectedDocument
      {
         get { return (OOLib.OfficeDocument)GetValue(SelectedDocumentProperty); }
         set { SetValue(SelectedDocumentProperty, value); }
      }

      // Using a DependencyProperty as the backing store for SelectedDocument.  This enables animation, styling, binding, etc...
      public static readonly DependencyProperty SelectedDocumentProperty =
          DependencyProperty.Register("SelectedDocument", typeof(OOLib.OfficeDocument), typeof(MainWindow), new UIPropertyMetadata(null));

      private void DocSaveButton_Click(object sender, RoutedEventArgs e)
      {
         if (SelectedDocument != null)
         {
            try
            { 
               SelectedDocument.Store();
            }
            catch (Exception ex)
            {
               Results.AppendText("Save failed\n" + ex.ToString() + "\n");
            }
         }
      }

      private void DocSaveAsButton_Click(object sender, RoutedEventArgs e)
      {
         if (SelectedDocument != null)
         {
            try
            {
               var dlg = new SaveFileDialog();
               if (dlg.ShowDialog(this) == true)
               {
                  SelectedDocument.StoreAsFileName(dlg.FileName);
               }
            }
            catch (Exception ex)
            {
               Results.AppendText("Save as failed\n" + ex.ToString() + "\n");
            }
         }
      }

      private void DocCloseButton_Click(object sender, RoutedEventArgs e)
      {
         if (SelectedDocument != null)
         {
            try
            {
               SelectedDocument.OODispose();
               Docs.Remove(SelectedDocument);
            }
            catch (Exception ex)
            {
               Results.AppendText("Close failed\n" + ex.ToString() + "\n");
            }
         }
      }
   }
}
